//
//  LCRequestManage.m
//  LendCash
//
//  Created by Mac mini on 2018/5/29.
//  Copyright © 2018年 zt. All rights reserved.
//

#import "LCRequestManage.h"

@implementation LCRequestManage

+(instancetype)sharedInstance{
    static LCRequestManage *manage = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manage = [[self alloc]init];
    });
    
    return manage;
}

-(void)afPOST:(NSString *)url andParam:(NSDictionary *)param success:(LCSuccessHandler)success failture:(LCErrorHandler)failture{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 5;
    
    
    
    [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        //
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        success(dic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failture(error);
    }];
}

-(void)afGET:(NSString *)url andParam:(NSDictionary *)param success:(LCSuccessHandler)success fail:(LCErrorHandler)fail{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 5;
    
    [manager GET:url parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        //
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        success(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(error);
    }];
}

-(void)uploadImagesWithUrl:(NSString *)url andImages:(NSArray *)images andParam:(NSDictionary *)param success:(LCSuccessHandler)success fail:(LCErrorHandler)fail{
    
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:url parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        if (images) {
            for (int i = 0; i<images.count; i++) {
                UIImage *image = images[i];
                NSData *data = [BFPublicDefTool reSizeImage:image maxSizeWithKB:2048];
//                NSData *data = UIImageJPEGRepresentation(image, 1.0);
                NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
                formatter.dateFormat = @"yyyyMMddHHmmss";
                NSString *str = [formatter stringFromDate:[NSDate date]];
                NSString *fileName = [NSString stringWithFormat:@"%@%d.png",str,i];
                [formData appendPartWithFileData:data name:@"images" fileName:fileName mimeType:@"image/png"];
            }
        }
    } error:nil];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSURLSessionUploadTask *uploaTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
//        dispatch_async(dispatch_get_main_queue(), ^{
//
//        });
    } completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            fail(error);
        }else{
            success(responseObject);
        }
    }];
    
    [uploaTask resume];
}

@end
